package com.caishichang.infosysdesign.controller.system;

import com.caishichang.infosysdesign.controller.vo.StrategyDetailVO;
import com.caishichang.infosysdesign.service.StrategyService;
import com.caishichang.infosysdesign.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping({"", "/strategy", "/index"})
public class StrategyController {

    public static String theme = "amaze";

    @Autowired
    StrategyService strategyService;

    /**
     * 首页
     *
     * @return
     */
    @GetMapping({"/", ""})
    public String index(HttpServletRequest request) {
        return this.page(request, 1);
    }

    /**
     * 首页 分页数据
     *
     * @return
     */
    @GetMapping({"/page/{pageNum}"})
    public String page(HttpServletRequest request, @PathVariable("pageNum") int pageNum) {
        PageResult strategyPageResult = strategyService.getStrategiesForIndexPage(pageNum);
        if (strategyPageResult == null) {
            return "error/error_404";
        }

        request.setAttribute("strategyPageResult", strategyPageResult);
        request.setAttribute("newStrategies", strategyService.getStrategiesForIndexPage(1));
        request.setAttribute("pageName", "守望先锋管理系统 | 攻略首页");
        return "public/" + theme + "/index";
    }

    /**
     * 查看攻略详情
     *
     * @param request
     * @param strategyId
     * @return
     */
    @GetMapping({"/strategy/{strategyId}"})
    public String detail(HttpServletRequest request, @PathVariable("strategyId") Long strategyId) {
        StrategyDetailVO strategyDetailVO = strategyService.getStrategyDetail(strategyId);
        if (strategyDetailVO != null) {
            request.setAttribute("strategyDetailVO", strategyDetailVO);
        } else {
            return "error/error_400";
        }

        request.setAttribute("pageName", "详情");
        return "public/" + theme + "/detail";
    }


    @GetMapping("/search/{keyword}")
    public String search(HttpServletRequest request, @PathVariable("keyword") String keyword) {
        return search(request, keyword, 1);
    }


    @GetMapping("/search/{keyword}/{page}")
    public String search(HttpServletRequest request, @PathVariable("keyword") String keyword,
                         @PathVariable("page") int page) {

        // 查询并分页的结果
        PageResult strategyPageResult = strategyService.getStrategiesPageBySearch(keyword, page);
        request.setAttribute("strategyPageResult", strategyPageResult);
        request.setAttribute("pageName", "搜索");
        request.setAttribute("pageUrl", "search");
        request.setAttribute("keyword", keyword);
        request.setAttribute("newStrategys", strategyService.getStrategiesForIndexPage(1));
        return "public/" + theme + "/list";
    }

}
